

#pragma once

#include "zAdAChar.h"
#include "zxgraph.h"

Ucad::ErrorStatus ucdbResolveCurrentXRefs(UcDbDatabase* pHostDb,
    bool useThreadEngine = true, bool doNewOnly = false);

Ucad::ErrorStatus ucdbAttachXref(UcDbDatabase* pHostDb,
                                 const ZTCHAR * pFilename,
                                 const ZTCHAR * pBlockName,
                                 UcDbObjectId& xrefBlkId);
Ucad::ErrorStatus ucdbOverlayXref(UcDbDatabase* pHostDb,
                                  const ZTCHAR * pFilename,
                                  const ZTCHAR * pBlockName,
                                  UcDbObjectId& xrefBlkId);
Ucad::ErrorStatus ucdbDetachXref(UcDbDatabase* pHostDb,
                           const UcDbObjectId& xrefBlkId);
Ucad::ErrorStatus ucdbUnloadXrefs(UcDbDatabase*      pHostDb,
                            const UcDbObjectIdArray& xrefBlkIds,
                            const bool               bQuiet = true);

Ucad::ErrorStatus ucdbReloadXrefs(UcDbDatabase*      pHostDb,
                            const UcDbObjectIdArray& xrefBlkIds,
                                  bool               bQuiet = true);

Ucad::ErrorStatus ucdbBindXrefs(UcDbDatabase*      pHostDb,
                          const UcDbObjectIdArray& xrefBlkIds,
                          const bool               bInsertBind,
                          const bool               bAllowUnresolved = false,
                          const bool               bQuiet = true);

Ucad::ErrorStatus ucdbXBindXrefs(UcDbDatabase*     pHostDb,
                           const UcDbObjectIdArray xrefSymbolIds,
                           const bool              bInsertBind,
                           const bool              bQuiet = true);
void ucdbReleaseHostDwg(UcDbDatabase* pHostDb);

Ucad::ErrorStatus
ucdbGetHostDwgXrefGraph(UcDbDatabase* pHostDb, UcDbXrefGraph& graph,
    ZSoft::Boolean includeGhosts = ZSoft::kFalse);

class UcDbXrefObjectId {
public:

    UcDbXrefObjectId();
    UcDbXrefObjectId(const UcDbXrefObjectId& other);

    UcDbXrefObjectId& operator= (const UcDbXrefObjectId& other);
    bool operator== (const UcDbXrefObjectId& other) const;
    bool operator!= (const UcDbXrefObjectId& other) const;

    bool isValid(void) const;
    bool isXref(void) const { return !m_handle.isNull(); }
    bool isNull(void) const { return m_localId.isNull(); }

    Ucad::ErrorStatus setNull(void);

    Ucad::ErrorStatus setXrefId(UcDbObjectId xrefBlkId, const UcDbHandle & hObject);
    Ucad::ErrorStatus getXrefId(UcDbObjectId& xrefBlkId, UcDbHandle& hObject) const;

    Ucad::ErrorStatus setLocalId(UcDbObjectId objId);
    Ucad::ErrorStatus getLocalId(UcDbObjectId& objId) const;

    Ucad::ErrorStatus resolveObjectId(UcDbObjectId& id) const;

    Ucad::ErrorStatus serializeToResbuf(resbuf*& pResBuf, resbuf*& pEndOfChain) const;
    Ucad::ErrorStatus serializeFromResbuf(const resbuf* pResBuf, resbuf*& pNextInChain);

private:

    UcDbObjectId        m_localId;
    UcDbHandle          m_handle;
                                        
};

